<?php
header('Content-Type:text/html;charset=utf-8');
date_default_timezone_set('PRC');
error_reporting(E_ERROR);
define('PATH_DOWNLOAD', './download/');
if(!file_exists(PATH_DOWNLOAD))
{
	mkdir(PATH_DOWNLOAD);
}
set_time_limit(0);
ob_end_clean();
echo str_pad('', 1024);
$url = 'http://www.manmankan.com/html/1/';
if(!function_exists('curl_init'))
{
	echo '<p><font color="red">执行失败!请先安装curl扩展!</font></p>';
	exit();
}
if(!preg_match('/^https?:\/\/[a-zA-Z0-9\-\.]+/i', $url))
{
	echo '<p><font color="red">执行失败!URL不合法!</font></p>';
	exit();
}
?>
<script>
	setInterval(function(){document.body.scrollTop = document.body.scrollHeight;}, 2000);
</script>
<?php
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_TIMEOUT, '30');
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.122 Safari/534.30");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
$string = iconv('gb2312', 'UTF-8', curl_exec($curl));
$httpStatus = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$totaltime = curl_getinfo($curl, CURLINFO_TOTAL_TIME);
if($httpStatus == 200)
{
	preg_match('/<title>(.*?)<\/title>/i' ,$string, $title_result_array);
	echo '<li><font color="green">进入栏目:<font color="black">'. $title_result_array[1] . '</font>(' . $url . ')' .'成功，耗时:'. $totaltime .'s</font></li>';
	flush();
	if(!file_exists(PATH_DOWNLOAD . iconv('UTF-8', 'GBK', $title_result_array[1])))
	{
		if(@mkdir(PATH_DOWNLOAD . iconv('UTF-8', 'GBK', $title_result_array[1])))
		{
			echo '<li><font color="green">建立文件夹：<font color="blue"><b> ' . PATH_DOWNLOAD . $title_result_array[1] . '</b></font> 成功</font></li>';
		}
		else
		{
			'<li><font color="green">建立文件夹：' . PATH_DOWNLOAD . $title_result_array[1] . '失败，请检查权限设置</font></li>';
			exit();
		}
	}
}
else
{
	echo '<li><font color="red">进入栏目:'. $url .'失败，错误码:'. $httpStatus .'</font></li>';
	exit();
}
if(preg_match_all('/<a.*?href.*?\/html\/1\/([0-9]*?)\.asp.*?title="(.*?)".*?>/u', $string, $result, PREG_SET_ORDER))
{
	$num_result = count($result);
	echo '<li><font color="black">此漫画共有：<font color="blue"><b> ' . $num_result . ' </b></font>集</li>';
	flush();
}
else
{
	echo '<li><font color="red">获取图片总数失败，请检查正则式</font></li>';
	exit();
}

for ($i = 0; $i < $num_result; $i++)
{
	$chapter = $num_result - 1 - $i;
	curl_setopt($curl, CURLOPT_URL, 'http://manhua.manmankan.com/html/1/' . $result[$i][1] . '.asp');
	$page_string  = iconv('gb2312', 'UTF-8', curl_exec($curl));
	if(!$page_string)
	{
		echo '<li><font color="red">抓取第<font color="blue"><b> ' . $result[$i][2] . ' </b></font>页套图链接失败，将跳过此页</font></li>';
		flush();
		$error_info = fopen(PATH_DOWNLOAD . 'error.txt', 'ab');
		$time_now = date('Y-m-d H:i:s');
		fwrite($error_info, '抓取第 ' . $i . ' 页套图链接失败，时间：' . $time_now . " \r\n");
		fclose($error_info);
		continue;
	}
	echo '<li><font color="black">进入第<font color="blue"><b> ' . $result[$i][2] . ' </b></font>集，开始抓取漫画链接</font></li>';
	flush();
	preg_match_all('/var\s*?ArrayPhoto=new\s*?Array\((.*?)\);/u', $page_string, $img_name_str);
	$img_name_noquotation = str_replace('"', '', $img_name_str[1]);
	$img_name_array = explode(',', $img_name_noquotation[0]);
	if(!file_exists(PATH_DOWNLOAD . iconv('UTF-8', 'GBK', $title_result_array[1]) . '/' . iconv('UTF-8', 'GBK', $result[$i][2]))){
		if(mkdir(PATH_DOWNLOAD . iconv('UTF-8', 'GBK', $title_result_array[1]) . '/'  . iconv('UTF-8', 'GBK', $result[$i][2])))
		{
			echo '<li><font color="green">建立文件夹：<font color="blue"><b> ' . PATH_DOWNLOAD . $title_result_array[1] . '/' . $result[$i][2] . '</b></font> 成功</font></li>';
			flush();
		}
		else
		{
			echo '<li><font color="red">建立文件夹：' . PATH_DOWNLOAD . $title_result_array[1] . '/' . $result[$i][2] . '失败，请检查无权限，将跳过此套图</font></li>';
			flush();
			$error_info = fopen(PATH_DOWNLOAD . 'error.txt', 'ab');
			$time_now = date('Y-m-d H:i:s');
			fwrite($error_info, '建立 ' . $result[$i][2] . '的文件夹失败，时间：' . $time_now . " \r\n");
			fclose($error_info);
			continue;
		}
	}
	for($j = 0; $j <= count($img_name_array); $j++)
	{
		$a_image_link = 'http://18.manmankan.com' . $img_name_array[$j];
		curl_setopt($curl, CURLOPT_URL, $a_image_link);
		echo '<li><font color="black">进入此套图<font color="blue"><b> ' . $result[$i][2] . ' </b></font>页面成功，开始下载第<font color="blue"><b> ' . $j . ' </b></font>张图片...</font></li>';
		$pic_suffix_arr = explode('.', $img_name_array[$j]);
		$pic_suffix = $pic_suffix_arr[count($pic_suffix_arr) - 1];
		if(file_exists(PATH_DOWNLOAD . iconv('UTF-8', 'GBK', $title_result_array[1]) . '/'  . iconv('UTF-8', 'GBK', $result[$i][2]) . '/' . iconv('UTF-8', 'GBK', $result[$i][2]) . '_' . $j . '.' . $pic_suffix))
		{
			echo '<li><font color="red">第<font color="blue"><b> ' . $result[$i][2]. '_' .$j . ' </b></font>张图片已存在，将跳过此张图片</font></li>';
			flush();
			continue;
		}
		$image_re = curl_exec($curl);
		if(!$image_re)
		{
			echo '<li><font color="red">下载图片失败，图片地址：' . $a_image_link . '将跳过此图片</font></li>';
			flush();
			continue;
		}
		else
		{
			$picfile = fopen(PATH_DOWNLOAD . iconv('UTF-8', 'GBK', $title_result_array[1]) . '/'  . iconv('UTF-8', 'GBK', $result[$i][2]) . '/' . iconv('UTF-8', 'GBK', $result[$i][2]) . '_' . $j . '.' . $pic_suffix, 'wb');
			if(!fwrite($picfile, $image_re))
			{
				echo '<li><font color="red">保存图片失败，将跳过此图片</font></li>';
				flush();
				continue;
			}
			fclose($picfile);
			echo '<li><font color="green">保存第<font color="blue"><b> ' . $result[$i][2] . '_' . $j . ' </b></font>张图片成功</font></li>';
			flush();
		}
	}
}
curl_close($curl);












